$.fn.search = function (opt,value,text) {
    if( opt=='setValue' ){
        setSearchValue($(this),value,text);
        return;
    }
    var options = {};
    $.extend(options,opt);
    var search = $(this);
    search.data({options:options});
    search.addClass('search');

    var input = $('<input type="text" name="'+options.name+'_text" class="input" autocomplete="off">');
    if( options.placeholder ){
        input.attr('placeholder',options.placeholder);
    }
    if( options.width ){
        search.width(options.width);
        input.width(options.width);
    }

    search.append(input);
    search.append('<input type="hidden" name="'+options.name+'">');
    search.append('<span class="icon"><i class="fa fa-search"></i></span>');
    search.find('.icon').click(function (e) {
        e.stopPropagation();
        options.click();
    });
    search.find('input[type="text"]').focus().click(function (e) {
        e.stopPropagation();
    });
    if(options.change){
        search.find('input[type="text"]').bind('input propertychange',options.change);
    }
};

function setSearchValue(_this,value,text) {
    _this.find('input[type="text"]').val(text);
    _this.find('input[type="hidden"]').val(value);
}

/* tab start */
$.fn.tabs = function (options) {
    var tabs = $(this);
    if(options.height){
        tabs.find('.vui-tab-body').height(options.height);
    }
    tabs.find('.vui-tab-head li').click(function(){
        tabs.find('.vui-tab-head li').removeClass("active");
        $(this).addClass("active");
        tabs.find('.vui-tab-body .vui-tab-page').removeClass("active");
        $($(this).find('a').attr('tid')).addClass("active");
    });
};
/* tab end */

$.fn.combobox = function (opt,value,text) {

    if( opt=='setValue' ){
        setSearchValue($(this),value,text);
        return;
    }

    var options = {valueField:'value',textField:'text',width:240,emptyRow:false,direction:'down',disable:false,queryParams:{}};
    $.extend(options,opt);
    var combobox = $(this);

    if( options.url ){
        
        var requestData =$.extend(options.queryParams,{loginUserId:top.loginUserId,customerId:top.customerId,loginCompanyId:top.loginCompanyId,loginCompanyLogicalCode:top.loginCompanyLogicalCode});
        
        $.ajax({
            type:'POST',
            dataType:'json',
            url:app.BASE_API_URL+options.url,
            data:requestData,
            success:function (resp) {
                var data = resp.data;
                if( options.data ){
                    data = data.concat(options.data);
                }
                if( options.emptyRow ){
                    data.push({});
                }
                options.data = data;
                combobox.data({options:options});
            }
        });
    }else{
        combobox.data({options:options});
    }

    combobox.addClass('combobox');

    var input = $('<input type="text" readonly="readonly" name="'+options.name+'_text" class="input" autocomplete="off">');
    input.click(expendCombobox);
    if( options.width ){
        combobox.width(options.width);
        input.width(options.width);
    }

    combobox.append(input);
    combobox.append('<input type="hidden" name="'+options.name+'">');
    combobox.append('<span class="icon" style="font-size:22px;"><i class="fa fa-angle-down"></i></span>');
    combobox.find('.icon').click(expendCombobox);
    combobox.find('input[type="text"]').focus();

    function expendCombobox(event) {
        event.stopPropagation();
        var options = combobox.data().options;
        if( options.edit && !options.edit() ){
            return;
        }

        if( !expendCheck(combobox,options) )return;

        var comboxBody = $('<div class="combobox-body" tabindex="0"></div>');
        comboxBody.width(options.width);
        comboxBody.data(options);

        if(options.data && options.data.length>8){
            comboxBody.height(202);
        }

        if( (!options.data || options.data.length==0) && !options.noDefault ){
            var o = {};
            o[options.textField] = "无数据";
            options.data = [o];
        }

        $.each(options.data,function(i,row){
            var value = row[options.textField];
            value = value == undefined ? '':value;
            var combo = $('<div>'+value+'</div>');
            combo.data(row);
            combo.mousedown(comboItemClick);
            comboxBody.append(combo);
        });

        var comboPoint = app.getElementPoint(combobox);

        if( options.direction == 'up' ){
            var comboxBodyHeight = options.data.length > 8 ? 202 : options.data.length*25 + 2 ;
            comboxBody.css({"top":(comboPoint.y-comboxBodyHeight-3)+"px","left":comboPoint.x+"px"});
        }else{
            comboxBody.css({"top":(comboPoint.y+35)+"px","left":comboPoint.x+"px"});
        }
        $(document.body).append(comboxBody);
    }

    function comboItemClick() {
        var row = $(this).data();
        var options = $(this).parent().data();
        var combobox = $('input[name="'+options.name+'"]').parent();
        if(options.click)options.click(row[options.valueField],row[options.textField]);
        combobox.find('input[type="text"]').val(row[options.textField]);
        combobox.find('input[type="hidden"]').val(row[options.valueField]);
        combobox.find('i').removeClass("fa-angle-up").addClass("fa-angle-down");
        $(this).parent().remove();
        if(options.change)options.change(row);
    }

    function expendCheck(combobox,options) {
        if( options.disable ){
            return false;
        }

        var comboBody = combobox.find('.combobox-body');
        if( comboBody.length > 0 ){
            comboBody.remove();
            return false;
        }

        combobox.find('i').removeClass("fa-angle-down").addClass("fa-angle-up");

        return true;
    }
};